<template>
  <a-modal
    title="新增类型"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleClose"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item label="图标" prop="imageUrl">
        <a-upload
          name="file"
          :action="url"
          :headers="headers"
          @change="handleChange"
        >
          <a-button :disabled="form.imageUrl ? true : false">
            <a-icon type="upload" /> 上传
          </a-button>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item  label="名称" prop="nameType">
        <a-input v-model="form.nameType" placeholder="请输入名称" />
      </a-form-model-item>
      <a-form-model-item  label="类型" prop="sysType">
            <a-select v-model="form.sysType" placeholder="请选择类型">
              <a-select-option value="1">系统</a-select-option>
              <a-select-option value="0">普通</a-select-option>
            </a-select>
      </a-form-model-item>
      <a-form-model-item  label="编码" prop="codeType">
        <a-input v-model="form.codeType" placeholder="请输入编码" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 14 },
      url: this.FORM_ACTION.globalUrl + "/rapid/common/upload/uploadImage",
      form: {
        imageUrl: "",
        nameType: "", //类型名称
        sysType:"1",//类型
        codeType:"",//编码
      },
      rules: {
        imageUrl: [
          { required: true, message: "请上传类型图标", trigger: "blur" },
        ],
        nameType: [
          { required: true, message: "请输入类型名称", trigger: "blur" },
        ],
        sysType:[
          { required: true, message: "请选择类型", trigger: "change" },
        ],
        codeType:[
          { required: true, message: "请输入编码", trigger: "blur" },
        ]
      },
      headers: {
        authorization: "authorization-text",
      },
    };
  },
  created() {
    // 获取角色列表
    console.log(this.FORM_ACTION.globalUrl);
  },
  methods: {
    show() {
      this.visible = true;
    },

    handleOk() {
      let _this = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          _this.$http("/rapid/newsType/saveOrUpdate", _this.form).then((res) => {
            if (res.code === 200) {
              _this.$message.success('操作成功');
              _this.visible = false;
              _this.$emit("reload");
            } else {
              _this.$message.error(res.message);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose() {
      this.visible = false;
    },
    //上传文件
    handleChange(info) {
      console.log(info);
      this.fileList = [];
      if (info.file.response) {
        if (info.file.status == "removed") {
          this.form.imageUrl = "";
        } else {
          this.form.imageUrl = info.file.response.result.path;
        }
        console.log(this.form);
      }
    },
  },
};
</script>
